package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
//user 表里的数据是 rose ，123
public class Test3 {
    public static void main(String[] args) throws Exception {
        //method();//无注入问题
        //method2();


    }

    private static void method2() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/cgb2015";
        String user = "root";
        String psd = "root";
        Connection conn = DriverManager.getConnection(url, user, psd);
        Statement st = conn.createStatement();
        System.out.println("输入账号");
        String a = new Scanner(System.in).nextLine();
        System.out.println("输入密码");
        String b = new Scanner(System.in).nextLine();
        String sql="select * from user where name='"+a+"' and password='"+b+"'";
        ResultSet rs = st.executeQuery(sql);
        if (rs.next()){
            System.out.println("yes");
        }else {
            System.out.println("no");
        }
        //输入rose‘#跳过密码直接登录
    }

    public static void method() throws Exception {


            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/cgb2015";
            String user = "root";
            String psd = "root";
            Connection conn = DriverManager.getConnection(url, user, psd);
            Statement st = conn.createStatement();
            System.out.println("请输入用户名");
            String a = new Scanner(System.in).nextLine();
            System.out.println("请输入用密码");
            String b = new Scanner(System.in).nextLine();

            String sql = "select * from user where name='" + a + "' and password='" + b + "' ";
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                System.out.println("登陆成功");
            } else {
                System.out.println("登陆失败");
            }
            rs.close();
            st.close();
            conn.close();
        }
    }
